/*
 * Class name: 			ShoppingGroup
 * Class description: 	The ShoppingGroup class contains the information
 * 						for the various shared community groups of our application
 * Project:				GrocerWE
 */

package com.cse210.grocerwe.common;

import com.cse210.grocerwe.client.SGroup;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(detachable="true")
public class ShoppingGroup {

	//database primary key
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    
    //name of list
    @Persistent
    private String name;
    
    //description of list
    @Persistent
    private String description;
    
    //creator of list
    @Persistent
    private String creator;
    
    //is this a users private list?
    @Persistent
    private boolean privateList;
    
    /*
     * Constructors
     */
    public ShoppingGroup(String name, String creator, boolean isPrivate)
    {
    	this.name = name;
    	this.description = null;
    	this.creator = creator;
    	this.privateList = isPrivate;
    }
    public ShoppingGroup(String name, String description, String creator, boolean isPrivate)
    {
    	this.name = name;
    	this.description = description;
    	this.creator = creator;
    	this.privateList = isPrivate;
    }
    public ShoppingGroup(SGroup sg)
    {
    	this.name = sg.getName();
    	this.description = sg.getDescription();
    	this.creator = sg.getCreator();
    	this.privateList = sg.isPrivate();
    }
    
    /*
     * Getters
     */
    public String getKey()
    {
    	return KeyFactory.keyToString(key);
    }
    public String getKeyString()
    {
    	return key.toString();
    }
    public String getName()
    {
    	return name;
    }
    public String getDescription()
    {
    	return description;
    }
    public String getCreator()
    {
    	return creator;
    }
    public boolean isPrivate()
    {
    	return privateList;
    }
    
    
    /*
     * Setters
     */
    public void setName(String name)
    {
    	this.name = name;
    }
    public void setDescription(String description)
    {
    	this.description = description;
    }
    public void setCreator(String creator)
    {
    	this.creator = creator;
    }
    public void setPrivacy(boolean privacy)
    {
    	privateList = privacy;
    }
    
    /*
     * Group related functionality
     */
    public boolean canModify(String user)
    {
    	return user.equals(creator);
    }
    
    public SGroup toSGroup()
    {
    	return new SGroup(getKey(),name,description,creator,privateList);
    }
}
